# Bitstring CIL tools for processing C code.
# Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
#
# $Id$

PACKAGE		= @PACKAGE_NAME@
VERSION		= @PACKAGE_VERSION@

OCAMLFIND	= @OCAMLFIND@
OCAMLMKLIB	= @OCAMLMKLIB@
OCAMLDOC	= @OCAMLDOC@
INSTALL		= @INSTALL@

enable_coverage = @enable_coverage@

OCAMLCFLAGS	= -g
OCAMLCPACKAGES	= -package dynlink,unix,str,extlib,cil -I +camlp4 -I ..
OCAMLCLIBS	= $(OCAMLCPACKAGES) camlp4lib.cma -linkpkg ../bitstring.cma ../bitstring_persistent.cma
OCAMLOPTFLAGS	=
OCAMLOPTPACKAGES = $(OCAMLCPACKAGES)
OCAMLOPTLIBS	= $(OCAMLOPTPACKAGES) camlp4lib.cmxa -linkpkg ../bitstring.cmxa ../bitstring_persistent.cmxa

ifneq ($(enable_coverage),no)
OCAMLCLIBS	:= -I +bisect bisect.cma $(OCAMLCLIBS)
OCAMLOPTLIBS	:= -I +bisect bisect.cmxa $(OCAMLOPTLIBS)
endif

PP		= -pp "camlp4o -I .. bitstring.cma bitstring_persistent.cma pa_bitstring.cmo"

OCAMLDOCFLAGS	= -html -sort

all:		bitstring-import-c bitstring-import-c.opt

bitstring-import-c: bitstring_import_c.cmo
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCLIBS) $< -o $@

bitstring-import-c.opt: bitstring_import_c.cmx
	$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(OCAMLOPTLIBS) $< -o $@

test:

# Examples.
#
# To compile task_struct you'll need to grab a copy of the Linux
# kernel original header files and set the directory below.

#DEBUG		=
DEBUG		= --debug
LINUX_HEADERS	= linux-2.6.25.7-headers
LINUX_INCLUDES	= -I $(LINUX_HEADERS)

#EXAMPLES	= ext3 task_struct
EXAMPLES	= ext3

examples: $(EXAMPLES)

ext3: ext3.cmo
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCLIBS) $< -o $@

ext3.cmo: ext3.ml ext3.bmpp
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) $(PP) -c $< -o $@

ext3.bmpp: ext3.c bitstring-import-c
	rm -f $@.new
	./bitstring-import-c $(DEBUG) $< > $@.new
	mv $@.new $@

task_struct: task_struct.cmo
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCLIBS) $< -o $@

task_struct.cmo: task_struct.ml task_struct.bmpp
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) $(PP) -c $< -o $@

task_struct.bmpp: task_struct.c bitstring-import-c
	cd $(LINUX_HEADERS) && ln -sf asm-x86 asm
	rm -f $@.new
	./bitstring-import-c $(DEBUG) $(LINUX_INCLUDES) $< > $@.new
	mv $@.new $@

# Clean.

clean:
	rm -f core *~ *.cmi *.cmo *.cmx *.cma *.cmxa *.a *.o *.bmpp
	rm -f bitstring-import-c bitstring-import-c.opt
	rm -f ext3 task_struct

distclean: clean

# Install.

install:

# Standard rules.

.mli.cmi:
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) -c $<
.ml.cmo:
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) -c $<
.ml.cmx:
	$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(OCAMLOPTPACKAGES) -c $<

depend: .depend

.depend: bitstring_import_c.ml
	rm -f .depend
	$(OCAMLFIND) ocamldep $(OCAMLCPACKAGES) $^ > $@

ifeq ($(wildcard .depend),.depend)
include .depend
endif

.PHONY: depend dist check-manifest dpkg doc \
	print-examples print-tests examples test

.SUFFIXES:      .cmo .cmi .cmx .ml .mli .mll
